package action;

import java.util.List;

import web.action.Action;

import model.Album;
import model.Artist;
import model.Credit;
import model.Track;

import agent.DbAgent;

@SuppressWarnings("serial")
public class AlbumSaveAction extends Action {

	String released;
	String item;
	String catalog;
	String format;
	String artist;
	String title;
	List<String> trackPosition;
	List<String> trackTitle;
	List<String> trackDuration;
	List<String> creditRole;
	List<String> creditArtist;
	List<String> creditTracks;

	public String getReleased() {
		return released;
	}
	public void setReleased(String released) {
		this.released = released;
	}
	public String getItem() {
		return item;
	}
	public void setItem(String item) {
		this.item = item;
	}
	public String getCatalog() {
		return catalog;
	}
	public void setCatalog(String catalog) {
		this.catalog = catalog;
	}
	public String getFormat() {
		return format;
	}
	public void setFormat(String format) {
		this.format = format;
	}
	public String getArtist() {
		return artist;
	}
	public void setArtist(String artist) {
		this.artist = artist;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public List<String> getTrackPosition() {
		return trackPosition;
	}
	public void setTrackPosition(List<String> trackPosition) {
		this.trackPosition = trackPosition;
	}
	public List<String> getTrackTitle() {
		return trackTitle;
	}
	public void setTrackTitle(List<String> trackTitle) {
		this.trackTitle = trackTitle;
	}
	public List<String> getTrackDuration() {
		return trackDuration;
	}
	public void setTrackDuration(List<String> trackDuration) {
		this.trackDuration = trackDuration;
	}
	public List<String> getCreditRole() {
		return creditRole;
	}
	public void setCreditRole(List<String> creditRole) {
		this.creditRole = creditRole;
	}
	public List<String> getCreditArtist() {
		return creditArtist;
	}
	public void setCreditArtist(List<String> creditArtist) {
		this.creditArtist = creditArtist;
	}
	public List<String> getCreditTracks() {
		return creditTracks;
	}
	public void setCreditTracks(List<String> creditTracks) {
		this.creditTracks = creditTracks;
	}
	public String execute() throws Exception {
		/** add new */
		Album album = new Album(released, item, catalog, format, title, null, null, null);
		if (artist != null) {
			for (String token: this.artist.split(Album.AND)) {
				token = token.trim();
				if (!token.isEmpty()) {
					Artist artist = new Artist(token, null, null);
					album.addArtist(artist);
				}
			}
		}
		if (trackTitle != null) {
			for (int index = 0; index < trackTitle.size(); index++) {
				if ((trackTitle.get(index) != null) && !trackTitle.get(index).isEmpty()) {
					Track track = new Track(album, trackPosition.get(index), trackTitle.get(index), trackDuration.get(index));
					album.addTrack(track);
				}
			}
		}
		if (creditArtist != null) {
			for (int index = 0; index < creditArtist.size(); index++) {
				if ((creditArtist.get(index) != null) && !creditArtist.get(index).isEmpty()) {
					Credit credit = new Credit(album, creditRole.get(index), creditTracks.get(index), null);
					for (String token: this.creditArtist.get(index).split(Credit.AND)) {
						token = token.trim();
						if (!token.isEmpty()) {
							Artist artist = new Artist(token, null, null);
							credit.addArtist(artist);
						}
					}
					album.addCredit(credit);
				}
			}
		}
		DbAgent agent = DbAgent.getSole();
		agent.storeAlbum(album);
		return SUCCESS;
	}
}
